﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using ChanExt;
using C.Unite;
using C.Entities;

namespace ArtLand.OA.Project.Project
{
    public partial class Pro_DeliveryChange : CommonPageBase
    {
        public string ContentS = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txt_CUTNO.OnClientTriggerClick = Window1.GetSaveStateReference(txt_CUTNO.ClientID, txt_CUTNAM.ClientID, txt_ADDRES.ClientID)
                   + Window1.GetShowReference("/PublicModel/SelectCustom.aspx?r=1", "选择客户");

                txt_EmpName.OnClientTriggerClick = Window1.GetSaveStateReference(hid_EmpID.ClientID, txt_EmpName.ClientID)
                  + Window1.GetShowReference("/PublicModel/SelectUsers.aspx?single=1", "选择执行人");


                FormInfo.ShowTriggerWin(Window2, txt_PONBER, txt_VEDNNBNo, "/PublicModel/SelectPodata.aspx", "选择采购订单");

                DataBaseHelper.BindDrp(Drp_STATUS, EnumData.DataBaseEunm.Plan);
                btnClose.OnClientClick = ActiveWindow.GetHideReference();
                LoadData();
            }
        }

        protected void LoadData()
        {
            if (!string.IsNullOrEmpty(Request["id"]))
            {



                Proj_Delivery Plan = TbsDbSession.Default.From<Proj_Delivery>().Where(Proj_Delivery._.ID == int.Parse(Request["id"])).ToFirst();
                if (Plan != null)
                {
                    string combin = "0";
                    if (Plan.COMBIN.Value)
                    {
                        combin = "1";
                    }
                    txt_PONBER.Text = Plan.PONBER;
                    txt_CUTNO.Text = Plan.CUTNO;
                    txt_CUTNAM.Text = Plan.CUTNAM;
                    txt_ADDRES.Text = Plan.ADDRES;
                    txt_PLDATE.Text = Plan.PLDATE.Value.ToString("yyyy-MM-dd");
                    if (Plan.EDTIME.HasValue)
                        txt_EDTIME.Text = Plan.EDTIME.Value.ToString("yyyy-MM-dd");
                    //txt_STTIME.SelectedDate = Plan.STTIME.Value;
                    //txt_EDTIME.SelectedDate = Plan.EDTIME.Value;
                    txt_CONTET.Text = Plan.CONTET;
                    Drp_STATUS.SelectedValue = Plan.STATUS;
                    Rdo_COMBIN.SelectedValue = combin;
                    txt_REMARK.Text = Plan.REMARK;
                    txt_EmpName.Text = Plan.EmpName;
                    if (Plan.VEDNNBNo != null)
                        txt_VEDNNBNo.Text = Plan.VEDNNBNo;
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(Request["pid"]))
                {
                    Proj_Podata pc = TbsDbSession.Default.From<Proj_Podata>().Where(Proj_Podata._.ID == int.Parse(Request["pid"])).ToFirst();
                    if (pc != null)
                    {
                        txt_PONBER.Text = pc.PONBER;
                        // txt_CONNUB.Text = pc.CONNUB;
                    }
                }
            }
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            bool combin = false;
            int returnvalue = 0;
            Proj_Delivery Plan = new Proj_Delivery();
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                Plan.ID = int.Parse(Request["id"]);
                Plan.Attach();
                Plan.PONBER = txt_PONBER.Text;
                //Plan.CONNUB = txt_CONNUB.Text;
                Plan.CUTNAM = txt_CUTNAM.Text;
                Plan.CUTNO = txt_CUTNO.Text;
                Plan.CUTNAM = txt_CUTNAM.Text;
                Plan.ADDRES = txt_ADDRES.Text;
                Plan.PLDATE = Convert.ToDateTime(txt_PLDATE.Text);
                Plan.EDTIME = Convert.ToDateTime(txt_EDTIME.Text);
                // Plan.EDTIME = Convert.ToDateTime(txt_EDTIME.Text);
                Plan.CONTET = txt_CONTET.Text;
                Plan.STATUS = Drp_STATUS.SelectedValue;
                if (Rdo_COMBIN.SelectedValue == "1")
                    combin = true;
                Plan.COMBIN = combin;
                Plan.REMARK = txt_REMARK.Text;
                Plan.EmpName = txt_EmpName.Text;
                Plan.VEDNNBNo = txt_VEDNNBNo.Text;
                returnvalue = TbsDbSession.Default.Update<Proj_Delivery>(Plan);
            }
            else
            {
                Plan.DeAttach();
                Plan.SHPNUB = DFieldSN.SN(EnumData.DataSNEunm.运货计划编号);
                Plan.PONBER = txt_PONBER.Text;
                Plan.CUTNO = txt_CUTNO.Text;
                Plan.CUTNAM = txt_CUTNAM.Text;
                Plan.ADDRES = txt_ADDRES.Text;
                Plan.PLDATE = Convert.ToDateTime(txt_PLDATE.Text);
                Plan.EDTIME = Convert.ToDateTime(txt_EDTIME.Text);
                // Plan.EDTIME = Convert.ToDateTime(txt_EDTIME.Text);
                Plan.CONTET = txt_CONTET.Text;
                Plan.STATUS = Drp_STATUS.SelectedValue;
                if (Rdo_COMBIN.SelectedValue == "1")
                    combin = true;
                Plan.COMBIN = combin;
                Plan.REMARK = txt_REMARK.Text;
                Plan.Adder = CookInfo.EAdminTrueName();
                Plan.AddID = CookInfo.EAdminid();
                Plan.AddTime = DateTime.Now;
                Plan.EmpName = txt_EmpName.Text;
                Plan.VEDNNBNo = txt_VEDNNBNo.Text;
                returnvalue = TbsDbSession.Default.Insert<Proj_Delivery>(Plan);
            }
            if (returnvalue > 0)
            {
                Alert.Show("更新成功！", "提示", ActiveWindow.GetHideRefreshReference());
            }
        }

    }
}